package backs.messageHandler;

import backs.JSONChater.MyJSONSend;
import backs.Main;
import backs.bean.Message;

import java.util.*;
import java.util.logging.Handler;

public class MessageHandleCenter
{
    private static List<MessageHandler> handlerList=new ArrayList<MessageHandler>();
    private static Map<Integer,Map<Integer,Message>> messageList=new HashMap<>();


    public static void receiveMessage(Message message)
    {
        replaceMessage(message);
        Map<Integer,Message> groupMap=messageList.get(message.groupID);
        if(groupMap!=null)
        {
            Message oldMessage=groupMap.get(message.userID);
            if(oldMessage!=null)
            {
                groupMap.remove(message.userID);
                message.message=oldMessage.message+message.message;
            }
        }
        if(message.message.endsWith("#++"))
        {
            message.message=message.message.substring(0,message.message.length()-3);
            if(groupMap==null)
            {
                groupMap=new HashMap<>();
                messageList.put(message.groupID,groupMap);
            }
            groupMap.put(message.userID,message);

        }
        int index=0;
        while(index<handlerList.size())
        {
            MessageHandler handler=handlerList.get(index);
            try
            {
                handler.handleMessage(message);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Main.addError("MessageHandleCenter"+"-"+index,e.getMessage());
                MyJSONSend.sendToMe(e.getMessage());
            }
            if(!message.notHandled) break;
            index++;
        }
    }

    public static void receivePrivateMessage(int id,String message)
    {
        receiveMessage(new Message(id,message));
    }

    public static void receiveGroupMessage(int groupID,int userID,String message)
    {
        receiveMessage(new Message(2,groupID,userID,message));
    }

    public static void receiveDiscussMessage(int discussID,int userID,String message)
    {
        receiveMessage(new Message(3,discussID,userID,message));
    }

    public static void registerHandler(MessageHandler handler)
    {
        handlerList.add(handler);
        sortHandlers();
    }



    public static void deleteHandler(MessageHandler handler)
    {
        handlerList.remove(handler);
        sortHandlers();
    }

    public static void sortHandlers()
    {

        Collections.sort(handlerList, new Comparator<MessageHandler>()
        {
            @Override
            public int compare(MessageHandler o1, MessageHandler o2)
            {
                if (o1.getRightNum() == o2.getRightNum())
                {
                    return 0;
                }
                else
                {
                    return o1.getRightNum() < o2.getRightNum() ? 1 : -1;
                }
            }
        });
    }

    public static void replaceMessage(Message message)
    {
        message.message=message.message.replace("&#91;","[");
        message.message=message.message.replace("&#93;","]");
    }
}
